package com.seeyon.apps.kksso.common;

import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.util.HttpClientUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class createUrl {
    private static final Logger log = LoggerFactory.getLogger(createUrl.class);
    public static String oaUrl = AppContext.getSystemProperty("kksso.oaHost");
    public static String host = AppContext.getSystemProperty("kksso.host");
    public static String client_id = AppContext.getSystemProperty("kksso.client_id");
    public static String client_secret = AppContext.getSystemProperty("kksso.client_secret");
    public static String redirectUri = AppContext.getSystemProperty("kksso.redict_uri");
    /**
     * 生成登录重定向地址
     * @return
     */
    public String makeLoginUrl () {
        StringBuffer redirect_uri = new StringBuffer(this.oaUrl);
        redirect_uri.append("/seeyon/loginController.do?method=loginSso");
        StringBuffer loginUrl = new StringBuffer(this.host);
        loginUrl.append("/cas/oauth2.0/authorize?client_id=");
        loginUrl.append(client_id);
        loginUrl.append("&redirect_uri=");
        try {
            loginUrl.append(URLEncoder.encode(redirect_uri.toString(), "UTF-8"));
            loginUrl.append("&response_type=code");
            //http://10.1.28.173/cas/oauth2.0/authorize?client_id=key111&redirect_uri=http%3A%2F%2F192.168.1.97%3A8113%2Fseeyon%2FloginController.do%3Fmethod%3DloginSso&response_type=code
            log.info("登录oa地址"+this.oaUrl);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            loginUrl.append(redirect_uri);
        }
        return loginUrl.toString();
    }
    /**
     * 获取用户信息
     * @param access_token token
     * @return
     */
    public String getUserInfo (String access_token) throws IOException {
        StringBuffer userInfoUrl = new StringBuffer(this.host);
        userInfoUrl.append("/cas/oauth2.0/profile?access_token=");
        userInfoUrl.append(access_token);
        HttpClientUtil httpClientUtil = new HttpClientUtil();
        httpClientUtil.openGet(String.valueOf(userInfoUrl));
        httpClientUtil.send();
        log.info("请求获取用户信息成功");
        String responseString = httpClientUtil.getResponseJsonAsObject(String.class);
        log.info("获取用户信息返回" + responseString);
        return responseString;
    }
    /**
     * 获取token
     * @param code
     * @return
     */
    public String getToken(String code) throws IOException {
        StringBuffer tokenUrl = new StringBuffer(this.host);
        tokenUrl.append("/cas/oauth2.0/accessToken?client_id=");
        tokenUrl.append(this.client_id);
        tokenUrl.append("&client_secret=");
        tokenUrl.append(this.client_secret);
        tokenUrl.append("&redirect_uri=");
        tokenUrl.append(URLEncoder.encode(redirectUri,"UTF-8"));
        tokenUrl.append("&grant_type=authorization_code");
        tokenUrl.append("&code=");
        tokenUrl.append(code);
        log.info("获取token链接" + tokenUrl);
        HttpClientUtil httpClientUtil = new HttpClientUtil();
        httpClientUtil.openPost(String.valueOf(tokenUrl));
        httpClientUtil.send();
        log.info("发送成功");
        String responseString = httpClientUtil.getResponseJsonAsObject(String.class);
        log.info("获取token返回" + responseString);
        return responseString;
    }
}
